const app = getApp();
const api = require('../../utils/api.js');
const util = require('../../utils/util.js');
var loginStatus = wx.getStorageSync("loginStatus");
const {
  $Toast
} = require('../../components/base/base');


Page({

  data: {
    navBg: app.globalData.navBg,
    navColor: app.globalData.navColor,
    windowHeight: app.globalData.windowHeight - app.globalData.navHeight,
    navTop: app.globalData.navHeight,
    back: true,
    navBox: true,
    home: false,
    scrollHeight: app.globalData.windowHeight - app.globalData.navHeight,
    goTop: true,
    confirmA: true,
    confirmB: false,
    confirmC: false
  },

  scroll: function (e) {
    var that = this;
    var listIndex = (e.detail.scrollTop + this.data.screenHeight) / (this.data.screenWidth * 0.4)
    if (e.detail.scrollTop > app.globalData.navHeight + 40) {
      this.setData({
        goTop: false
      });
      this.setData({
        listIndex: listIndex
      })
    } else {
      this.setData({
        goTop: true
      });
    }

  },

  //返回顶部
  goScrolltop: function (e) {
    var that = this;
    var top = that.data.scrollTop;
    if (top == 1) {
      top = 0;
    } else {
      top = 1;
    }

    that.setData({
      scrollTop: top
    })

  },

  confirmA: function(){
    this.setData({
      confirmA: true,
      confirmB: false,
      confirmC: false
    })
  },

  confirmB: function () {
    this.setData({
      confirmA: false,
      confirmB: true,
      confirmC: false
    })
  },

  confirmC: function () {
    this.setData({
      confirmA: false,
      confirmB: false,
      confirmC: true
    })
  },

  GetShipInfo: function(){
    var that = this;
    wx.request({
      url: util.api + api.GetShipInfo,
      data: {
        BUSINESS_PARAMETERS: {
          userToekn: loginStatus.token,
          PostStaId: 22
        },
        SYSTEM_PARAMETERS: {
          APP_ID: "M_P_G001",
          METHOD_ID: 0,
          ACCESS_TOKEN: "1w2Re3bE^og38",
          TIMESTAMP: util.formatTime(new Date(), 'Y/M/D h:m:s'),
          SIGN: "string"
        }
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        if (res.statusCode == 200) {
          if (res.data.MESSAGE.MESSAGE_CODE == 1) {
            var confirm = [];
            for(var i=0;i<res.data.RESULT.length;i++){
              confirm.push(res.data.RESULT[i])
            }
            that.setData({
              confirmlist: res.data.RESULT.CartProList,
              Weight: res.data.RESULT.Weight
            })
            console.log(res.data.RESULT)
          }
        }
        
      },
      fail: function (res) {
        
      }

    })
  },

  GetAddressList: function(){
    var that = this;
    wx.request({
      url: util.api + api.GetAddressList,
      data: {
        BUSINESS_PARAMETERS: {
          userToekn: loginStatus.token,
          PostStaId: 22
        },
        SYSTEM_PARAMETERS: {
          APP_ID: "M_P_G001",
          METHOD_ID: 0,
          ACCESS_TOKEN: "1w2Re3bE^og38",
          TIMESTAMP: util.formatTime(new Date(), 'Y/M/D h:m:s'),
          SIGN: "string"
        }
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        
        var address = [];
        for(let i=0;i<res.data.RESULT.length;i++){
          if(res.data.RESULT[i].isDefault == true){
            address.push(res.data.RESULT[i])
          }
        }
        that.setData({
          UserName: address[0].UserName,
          Address: address[0].Address,
          Mobile: address[0].Mobile
        });
      },
      fail: function (res) {
        
      }

    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var arr = wx.getStorageSync('shopCart') || [];
    var confirm = [];
    var num = null;
    var prices = null;
    for(var i=0;i<arr.length;i++){
      if (arr[i].checked == true){
        confirm.push(arr[i])
        num += arr[i].count;
        prices += parseFloat(arr[i].count * arr[i].price)
      }
    }
    console.log(prices)
    that.setData({
      points: prices,
      number: num
    })
    that.GetShipInfo()
    that.GetAddressList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }

})